﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Com.Pirate.Domain.Services;
using Com.Pirate.Domain.Services.Contracts;
using Com.Pirate.Common.Contexts;
using Com.Pirate.Domain.Objects.Contracts;

namespace Com.Pirate
{
    public partial class UserInfo : Form
    {
        IuserinfoService service = null;
        Dictionary<string, Iuserinfo> userInfoList = new Dictionary<string, Iuserinfo>();
        public UserInfo()
        {
            InitializeComponent();
        }

        private void UserInfo_Load(object sender, EventArgs e)
        {
            try
            {
                DomainServicesContext context = new DomainServicesContext();
                service = (IuserinfoService)context.GetObject("userinfoService");
                Init();
            }
            catch(Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var info = UIToEntity();
            service.Insert(info);
            Init();
            MessageBox.Show("Done");
        }

        private void cbxUserList_SelectedIndexChanged(object sender, EventArgs e)
        {
            var userInfo=userInfoList[cbxUserList.SelectedItem.ToString()];
            EntityToUI(userInfo);
        }

        private void Init()
        {
            var list = service.GetList<Iuserinfo>("", null);
            userInfoList.Clear();
            cbxUserList.Items.Clear();
            foreach (Iuserinfo info in list)
            {
                userInfoList.Add(info.Id, info);
                cbxUserList.Items.Add(info.Id);
            }
            if (list.Count > 0)
            {
                cbxUserList.SelectedItem = list[0].Id;
                EntityToUI(list[0]);
            }
        }

        private void EntityToUI(Iuserinfo info)
        {
            this.tbxFirstName.Text = info.FirstName;
            this.tbxSecondName.Text = info.SecondName;
            this.tbxLastName.Text = info.FamilyName;
            this.tbxUserId.Text = info.Id;
        }

        private Iuserinfo UIToEntity()
        {
            Iuserinfo info = new Com.Pirate.Domain.Objects.userinfo();
            info.Id = this.tbxUserId.Text;
            info.FirstName = this.tbxFirstName.Text;
            info.SecondName = this.tbxSecondName.Text;
            info.FamilyName = this.tbxLastName.Text;
            return info;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            var userInfo=UIToEntity();
            if (userInfo.Id == cbxUserList.SelectedItem.ToString())
            {
                service.Update(userInfo);
                MessageBox.Show("Done");
            }
            else
            {
                MessageBox.Show("you can't change the id!");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            var userInfo = UIToEntity();
            service.Delete(userInfo);
            Init();
            MessageBox.Show("Done");

        }

    }
}
